import React, { useEffect } from "react";
import { Card, Form, Input, Button, message } from "antd";

import type {
  getHosSetListItemType,
  updateHosSetParamsType,
  addHosSetParamsType,
} from "@api/hospital/model/hospitalSetTypes";

import {
  getHosSetFormIdRequest,
  addHosSetFormRequest,
  updateHosSetRequest,
} from "@api/hospital/hospitalSet";

import style from "../index.module.css";
import { useNavigate, useParams } from "react-router-dom";

export default function AddOfUpdateHosSet() {
  const navigate = useNavigate();
  const [form] = Form.useForm();
  const { id } = useParams();

  useEffect(() => {
    const getHosDetail = async () => {
      const re = await getHosSetFormIdRequest(id);
      form.setFieldsValue(re);
    };
    getHosDetail();
  }, []);

  const onFinish = async (fieldValue: addHosSetParamsType) => {
    if (id) {
      await updateHosSetRequest({ ...fieldValue, id });
    } else {
      await addHosSetFormRequest(fieldValue);
    }
    message.success(id?'更新成功':'添加成功')
    navigate("/syt/hospital/hospitalSet");
  };
  return (
    <div>
      <Card>
        <Form
          name="basic"
          form={form}
          labelCol={{ span: 3 }}
          wrapperCol={{ span: 18 }}
          onFinish={onFinish}
        >
          <Form.Item label="医院名称" name="hosname">
            <Input />
          </Form.Item>
          <Form.Item label="医院编号" name="hoscode">
            <Input />
          </Form.Item>
          <Form.Item label="api基础路径" name="apiUrl">
            <Input />
          </Form.Item>
          <Form.Item label="联系人姓名" name="contactsName">
            <Input />
          </Form.Item>
          <Form.Item label="联系人手机" name="contactsPhone">
            <Input />
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 3 }}>
            <Button type="primary" htmlType="submit" className={style.mr}>
              提交
            </Button>
            <Button
              type="default"
              onClick={() => {
                navigate(-1);
              }}
            >
              返回
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
}
